package com.cloud.school.bus.teacherhelper.modules.home;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.android.support.jhf.touchimageview.TouchImageView;
import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.ImageUtil;
import com.android.support.jhf.utils.ThumbnailUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.entitys.Picture;
import com.cloud.school.bus.teacherhelper.modules.home.camera.CustomCameraActivity;

/**
 * 照相完成之后显示大图 flag 0表示照完相，1表示上传照片界面调用
 * 
 * @author jiahongfei
 * 
 */
public class CameraBigPictureActivity extends BaseActivity {

	private int mFlag = 0;
	private String picPathString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_camera_big_picture);

		picPathString = getIntent().getStringExtra("picPath");
		mFlag = getIntent().getIntExtra("flag", 0);
		
		mApplication.imageLoaderInit(480, 800);

		TouchImageView touchImageView = (TouchImageView) findViewById(R.id.touchImageView);
		if (!TextUtils.isEmpty(picPathString)) {
			ImageLoader.getInstance()
					.displayImage(
							Uri.decode(Uri.fromFile(new File(picPathString))
									.toString()), touchImageView,
							mApplication.mNoCacheDisplayImageOptions,
							new SimpleImageLoadingListener() {
								@Override
								public void onLoadingComplete(String imageUri,
										View view, Bitmap loadedImage) {
									
									int degree = ImageUtil.readPictureDegree(imageUri.replace("file:///", "/"));
									Bitmap tempBitmap = null;
									if (Math.abs(degree) > 0) {
										tempBitmap = ImageUtil.rotaingImageView(degree, loadedImage);
										
									}else {
										tempBitmap = loadedImage;
									}
									
									TouchImageView tempTouchImageView = (TouchImageView) view;
									tempTouchImageView
											.setImageBitmap(tempBitmap);
								}
							});
		}
		Button cancelButton = (Button) findViewById(R.id.cancelButton);
		Button play_cancel = (Button) findViewById(R.id.play_cancel);
		Button play_next = (Button) findViewById(R.id.play_next);
		if (0 == mFlag) {
			play_cancel.setVisibility(View.VISIBLE);
			cancelButton.setVisibility(View.GONE);
			play_next.setVisibility(View.VISIBLE);
		} else if (1 == mFlag) {
			play_cancel.setVisibility(View.GONE);
			cancelButton.setVisibility(View.VISIBLE);
			play_next.setVisibility(View.GONE);
		}
		cancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				cancel();
			}
		});
		play_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				File file = new File(picPathString);
				file.delete();
				Intent intent = new Intent(mContext, CustomCameraActivity.class);
				startActivity(intent);
				cancel();
			}
		});
		play_next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Picture picture = new Picture();
				picture.setPicturePath(picPathString);
				Intent intent = new Intent(mContext,
						UploadFileStuSelectedActivity.class);
				intent.putExtra("title", getString(R.string.upload_photo));
				intent.putExtra("index", -2);
				intent.putExtra("picture", picture);
				startActivityForResult(intent, 0);
				cancel();
			}
		});

	}
	
	private void cancel() {
		mApplication.imageLoaderInit(80, 80);
		finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (0 == mFlag) {
				File file = new File(picPathString);
				file.delete();
				Intent intent = new Intent(mContext, CustomCameraActivity.class);
				startActivity(intent);

			} else if (1 == mFlag) {
			}
			cancel();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
